package yazan.com.pdacollector_android.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.device.ScanDevice;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Looper;

import com.blankj.utilcode.util.ToastUtils;

import java.io.IOException;

public class ScanDeviceHelper extends BroadcastReceiver {

    public interface ScanDeviceHelperListener {
        void onSuccess(String content);
    }

    private ScanDevice scanDevice;
    private Context context;
    private ScanDeviceHelperListener listener;
    private MediaPlayer mediaPlayer = new MediaPlayer();

    public ScanDeviceHelper(Context context, ScanDeviceHelperListener listener) {
        this.context = context;
        this.listener = listener;
        scanDevice = new ScanDevice();
        scanDevice.openScan();
        scanDevice.setScanUnBeep();
        scanDevice.setOutScanMode(0);
    }


    /**
     * 语音播报 1 成功 2 失败 3 不能超过最大数
     *
     * @param code .
     */
    public void speak(final int code) {
        synchronized (this) {
            try {
                if (mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();
                }
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                    mediaPlayer = new MediaPlayer();
                }
                mediaPlayer.reset();
                String fileName = "fail.mp3";
                switch (code) {
                    case 1:
                        fileName = "success.mp3";
                        break;
                    case 2:
                        fileName = "fail.mp3";
                        break;
                    case 3:
                        fileName = "max_limit.mp3";
                        break;
                }
                AssetManager assetManager = context.getAssets();
                AssetFileDescriptor fd = assetManager.openFd(fileName);
                mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                    }
                });
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void startScan() {
        if (scanDevice.isScanOpened()) {
            scanDevice.startScan();
        } else {
            ToastUtils.showShort("请打开扫描模式!");
        }
    }

    public void stopScan() {
        scanDevice.stopScan();
    }

    public void registerReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("scan.rcv.message");
        context.registerReceiver(this, intentFilter);
    }

    public void unRegisterReceiver() {
        if (scanDevice != null) {
            scanDevice.stopScan();
        }
        context.unregisterReceiver(this);
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        byte[] barocode = intent.getByteArrayExtra("barocode");
        int barocodelen = intent.getIntExtra("length", 0);
        byte temp = intent.getByteExtra("barcodeType", (byte) 0);
        final String result = new String(barocode, 0, barocodelen);
        if (listener != null) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    listener.onSuccess(result);
                }
            });
        }
        stopScan();
    }
}
